package com.njzxw.fs.serversys.repository;


import com.njzxw.fs.commonutil.bean.BaseBean;
import com.njzxw.fs.commonutil.db.ZxwLangDriver;
import com.njzxw.fs.serversys.bean.MenuBean;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface MenuReq {

    /**
     * 获取全部菜单
     * @return
     */
    @Select("SELECT\n" +
            "\tt_manager_menu.menu_url,\n" +
            "\tt_manager_menu.levels,\n" +
            "\tt_manager_menu.icon,\n" +
            "\tt_manager_menu.supper_id,\n" +
            "\tt_manager_menu.orderby,\n" +
            "\tt_manager_menu.isdelete,\n" +
            "\tt_manager_menu.id,\n" +
            "\tt_manager_menu.menu_name \n" +
            "FROM\n" +
            "\tt_manager_menu \n" +
            "WHERE\n" +
            "\tt_manager_menu.state = 1 order by t_manager_menu.orderby")
    List<Map<String,Object>> getMenu();

    /**
     * 获取全部菜单对应的全部权限
     * @return
     */
    @Select("select * from t_manager_menu_authority a where a.state = 1 order by a.id ")
    List<Map<String,Object>> getMenuAuthority();

    @Select("select * from t_manager_menu a where a.state = 1 order by a.orderby  ")
    List<Map<String,Object>> getMenuData();

    @Select("SELECT a.menu_id,a.name,a.id,a.isdelete FROM t_manager_menu_authority a WHERE a.state = 1")
    List<Map<String,Object>> getPowerData();

    @Select("SELECT a.id,a.role_name,a.remark,a.isdelete,a.level FROM t_manager_role a WHERE a.state = 1")
    List<Map<String,Object>> getRoleData();

    @Select("SELECT * FROM t_manager_role a,t_manager_role_authority b\n" +
            "\t\t\tWHERE a.id = b.role_id\n" +
            "\t\t\t AND b.authority_id = #{menu_id}\n" +
            "\t\t\t and a.state = 1")
    List<Map<String,Object>> loadMenuRole(MenuBean menuBean);

    @Select("SELECT * FROM t_manager_role a,t_manager_role_authority b,t_manager_menu_authority c\n" +
            "\t\t\tWHERE a.id = b.role_id\n" +
            "\t\t\t AND b.authority_id = c.id\n" +
            "\t\t\t and a.id = #{role_id}\n" +
            "\t\t\t and a.state = 1")
    List<Map<String,Object>> loadRoleMenu(MenuBean menuBean);

    @Select("select * from t_manager_menu a where a.state = 1 and a.id = #{menu_id}")
    Map<String,Object> getMenuMap(MenuBean menuBean);

    @Select("select count(1) from t_manager_menu a where a.supper_id = #{menu_id}")
    int checkMenuSupper(MenuBean menuBean);

    @Delete("delete a from t_manager_menu a where a.id = #{menu_id}  AND a.isdelete = 1")
    int deleteMenu(MenuBean menuBean);

    @Delete("delete a from t_manager_menu_authority a where a.menu_id = #{menu_id}  AND a.isdelete = 1")
    int delereMenuPower(MenuBean menuBean);

    @Delete("delete a from t_manager_menu_authority a where a.menu_id = #{menu_id}  AND a.isdelete = 1 and a.url is null ")
    int delereMenuPowerNull(MenuBean menuBean);

    @Delete("delete a FROM t_manager_role_authority a WHERE exists(select 1 from t_manager_menu_authority b where b.id = a.authority_id and b.menu_id = #{menu_id})")
    int deleteRolePower(MenuBean menuBean);

    @Select("SELECT COUNT(1) FROM t_manager_role a WHERE a.isdelete = 1 AND a.id = #{role_id}")
    int checkDeleteRole(MenuBean menuBean);

    @Update("UPDATE t_manager_role a SET a.state = 0 where a.id = #{role_id}")
    int deleteRole(MenuBean menuBean);

    @Delete("DELETE a FROM t_manager_role_authority a where a.role_id = #{role_id}")
    int deleteRolePowerRole(MenuBean menuBean);

    @Insert("INSERT into t_manager_role_authority(role_id,authority_id)VALUES(#{role_id},#{authority_id})")
    int insertRolePower(MenuBean menuBean);

    @Delete("DELETE a FROM t_manager_role_authority a where a.authority_id = #{authority_id}")
    int deleteRolePowerAuthority(MenuBean menuBean);

    @Select("select count(1) FROM t_manager_role a WHERE a.role_name = #{role_name} and a.state = 1")
    int checkRoleName(MenuBean menuBean);

    @Insert("INSERT INTO t_manager_role(role_name,level)VALUES(#{role_name},#{levels})")
    int addRole(MenuBean menuBean);

    @Update("update t_manager_menu a set a.menu_url = null where a.id = #{menu_id}")
    int updateSupperMenuUrl(MenuBean menuBean);

    @Insert({"insert into t_manager_menu(#{menuBean})"})
    @Options(useGeneratedKeys = true)
    @Lang(ZxwLangDriver.class)
    int insertMenu(MenuBean menuBean);

    @Update("update t_manager_menu a (#{menuBean}) where a.id = #{menu_id}")
    @Lang(ZxwLangDriver.class)
    int updateMenu(MenuBean menuBean);

    @Delete("delete a from t_manager_menu_authority a  WHERE a.id = #{power_id} AND a.state = 1")
    int deletePower(MenuBean menuBean);

    @Select("select count(1) from t_manager_menu_authority a where a.state = 1 and a.url = #{url} and a.menu_id <> #{menu_id} ")
    @Lang(ZxwLangDriver.class)
    int checkPowerUrl(MenuBean menuBean);

    @Insert("insert into t_manager_menu_authority(name,menu_id,url)values(#{name},#{menu_id},#{url})")
    int insertMenuPower(MenuBean menuBean);

    @Update("update t_manager_menu_authority a set a.name=#{name},a.menu_id=#{menu_id},a.url = #{url} where a.id = #{id}")
    int updateMenuPower(MenuBean menuBean);

    @Select("select count(1) from t_manager_menu_authority a where a.menu_id = #{menu_id}")
    int selectPowerMenuCnt(MenuBean menuBean);

    @Select("select * from t_manager_menu_authority a where a.state = 1 and a.menu_id = #{menu_id}")
    List<Map<String,Object>> getMenuAuthorityList(MenuBean menuBean);

    @Select("select * from t_manager_menu a where a.state = 1 and ifnull(a.supper_id,0) = #{supper_id} and a.id = #{id} ")
    @Lang(ZxwLangDriver.class)
    List<Map<String,Object>> getMenuList(BaseBean baseBean);

    @Insert("insert into t_manager_user_workbench(oper_id,menu_id)values(#{oper_id},#{menu_id})")
    Integer insertWorkbench(MenuBean menuBean);

    @Insert("delete a t_manager_user_workbench a where a.oper_id = #{oper_id} and a.menu_id = #{menu_id}")
    Integer deleteWorkbench(MenuBean menuBean);

    @Select("select b.id,b.menu_url,b.menu_name\n" +
            "  from t_manager_user_workbench a,t_manager_menu b \n" +
            " where a.menu_id = b.id \n" +
            "   and a.oper_id = #{oper_id}\n")
    List<Map<String,Object>> getMenuWorkbenchList(BaseBean baseBean);
}
